package com.yzc.mihuyou.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
    //    后台监控
    @Bean
    public ServletRegistrationBean Ser(){
        ServletRegistrationBean bane= new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        HashMap<String,Object> intParameters=new HashMap<>();
        intParameters.put("loginUsername","admin");
        intParameters.put("loginPassword","123456");
        intParameters.put("allow","");
        bane.setInitParameters(intParameters);
        return  bane;
    }
    //filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bane=new  FilterRegistrationBean();
        bane.setFilter(new WebStatFilter());
        Map<String,String> intParameters= new HashMap<>();
        intParameters.put("exclusions","*.js,*.css,/druid/*");
        bane.setInitParameters(intParameters);
        return  bane;
    }
}
